﻿using Common.Contract.Notification;
using Common.Service.Notification;
using Microsoft.AspNet.SignalR;
using Quartz;
using Quartz.Impl;
using System;

namespace Common.Service
{
    public class NotificationProcessing : INotification
    {
        private static readonly ISchedulerFactory factory;
        private static readonly IScheduler scheduler;
        private static IJobDetail _jobDetail;
        static NotificationProcessing()
        {
            try
            {
                factory = new StdSchedulerFactory();
                scheduler = factory.GetScheduler();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool SenMessage(string name, string message)
        {
            try
            {
                scheduler.Start();
                ITrigger trigger = TriggerBuilder.Create()
                  .StartNow()  // Starts the trigger immediately when scheduled
                  .Build();  // Builds a trigger to assign to a job

                _jobDetail = JobBuilder.Create<NotificationJob>().Build();   // Build the job detail 
                // Ask the scheduler to schedule our EmailJob
                scheduler.ScheduleJob(_jobDetail, trigger);

                var cont = GlobalHost.ConnectionManager.GetHubContext<ChatHub>();
                cont.Clients.All.senMessageToPage(name, message);
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
